function xr() {//调取数据
    $.ajax({
        type: "get",
        url: 'data/dm.json',
        success: function (response) {
            var data = response.data;
            data = data.sort(function () { return Math.random() > 0.5 ? -1 : 1; });//第一次打乱排序
            var str = '';
            $.each(data, function (index, item) {
                str += `
                <li class="sdList">${item.name}</li>
                `;
            });
            $('.item').html(str);
        }
    });
}
xr();

// var time = $('#time').attr()
var num_ = 0;
//var  nameList = [];//
var loop = true;//节流锁
var name_ = [];//统计所选中过的人名
var n = null;//每次随机最后一个人名


$('.btn').on('click', function () { //开始随机按钮
    if (loop == true) {
        loop = false;
        var t = setInterval(function () {
            math();
        }, 10);
        setTimeout(function () {
            name_.push(n);
            JSON.stringify(localStorage.setItem("name", name_));
            xrName();
            clearInterval(t);
            loop = true;
        }, 1000);
    };
});

function math(num_) {// 选人并渲染
    var i = $('.sdList').length;
    num_ = Math.floor((Math.random() * i));//制造第二个随机数  再次随机排序
    $('.sdList').eq(num_).css({ background: 'pink', color: '#fff' }).siblings().css({ background: '#fff', color: '#fff' });
    n = $('.sdList').eq(num_).html();
}


$('.show').click(function () { //手动重排
    xr();
});



function xrName() {// 当天的选人统计
    nameList = localStorage.getItem("name");
    nameList = nameList.split(/,/)
    let str = '';
    if(nameList.length>0){
        $.each(nameList,function(index,item){
            str += `
            <div class="list">
            <span>${index+1} : </span>
            <span>${item}</span>
            </div>
            `;
        });
    };
    $('.nameList').html(str);
};